package com.witsight.hibernate.config;

import javax.persistence.EntityManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.witsight.hibernate.dao.CustomJpaRepositoryFactoryBean;

/**   
 * 说明：jpa hibernate Dao config
 * @Title: JpaDaoConfiguration.java 
 * @Package com.witsight.hibernate.config 
 * @See: {@link EnableJpaRepositories}
 *       {@link EnableJpaRepositories}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: fuyp  
 * @date: 2017年9月13日 上午8:05:21 
 * @version: V1.0
 *
 */
@Configuration
@ConditionalOnClass(JpaRepository.class)
@EnableTransactionManagement
@EnableJpaRepositories(repositoryFactoryBeanClass = CustomJpaRepositoryFactoryBean.class, 
	basePackages="com.**.dao.**", transactionManagerRef = "customTransactionManager", 
	enableDefaultTransactions = false)
@EntityScan("com.**.model.**")
@EnableJpaAuditing
@AutoConfigureAfter({ JpaAutoConfiguration.class })
public class JpaDaoConfiguration{
	private static final Logger log = LoggerFactory.getLogger(JpaDaoConfiguration.class);
//	/**
//	 * 
//	 * 说明：创建sessionFactory
//	 * @Title: sessionFactory
//	 * @See: {@link HibernateJpaSessionFactoryBean}
//	 * @author fuyp
//	 * @param emf EntityManagerFactory
//	 * @return SessionFactory
//	 * @date: 2017年9月18日 下午5:52:35
//	 */
//	@Bean
//	public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
//		if (log.isDebugEnabled()) {
//			log.debug("config custom repository.");
//		}
//		HibernateJpaSessionFactoryBean sessionFactoryBean = new HibernateJpaSessionFactoryBean();
//		sessionFactoryBean.setEntityManagerFactory(emf);
//	    return sessionFactoryBean;
//	}
	/**
	 * 
	 * 说明：配置JpaTransactionManager 事物管理
	 * @Title: transactionManager
	 * @See: {@link JpaTransactionManager}
	 * @author fuyp
	 * @param emf EntityManagerFactory
	 * @return JpaTransactionManager
	 * @date: 2017年9月18日 下午5:51:53
	 */
	@Bean(name = "customTransactionManager")
	public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
		if (log.isDebugEnabled()) {
			log.debug("config custom JpaTransactionManager.");
		}
		JpaTransactionManager transactionManager = new JpaTransactionManager();
		transactionManager.setEntityManagerFactory(emf);
		return transactionManager;
	}
}
